﻿using System.Collections.Generic;

public interface IZUpdate
{
    void ZUpdate();
}

public class UpdateMgr : Singleton<UpdateMgr>
{
    private static List<IZUpdate> updateList = new List<IZUpdate>();
    private static List<IZUpdate> removeList = new List<IZUpdate>();
    
    public void AddUpdate(IZUpdate update)
    {
        if (!updateList.Contains(update))
        {
            updateList.Add(update);
        }
        else if (removeList.Contains(update))
        {
            removeList.Remove(update);
        }
    }

    public void RemoveUpdate(IZUpdate update)
    {
        if (!removeList.Contains(update))
        {
            removeList.Add(update);
        }
    }

    public void RemoveUpdate()
    {
        while (removeList.Count > 0)
        {
            updateList.RemoveAt(0);
            removeList.RemoveAt(0);
        }
    }

    public void RunUpdate()
    {
        for (int i = 0; i < updateList.Count; i++)
        {
            IZUpdate update = updateList[i];
            if (!removeList.Contains(update))
            {
                update.ZUpdate();
            }
        }
    }
}